/**
 * created by 小卷毛, 2021-06-16
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
import com.rzico.sbl.R
import com.rzico.sbl.model.FilterData
import com.xinnuo.common.extend.*
import com.xinnuo.common.decoration.DividerLRDecoration
import com.xinnuo.common.view.DropPopWindow
import com.xinnuo.common.view.DropPopupWindow
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.slimadapter.SlimAdapter

/**
 * 描 述：下拉筛选框操作相关
 * 作 者：小卷毛  2021-06-16 17:03
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
open class DialogViewModel : CommonViewModel() {

    protected var dropFilter: PopupWindow? = null

    val mTimeList by lazy {
        listOf(
            FilterData("今日", "1", "今日", true),
            FilterData("昨日", "2", "昨日"),
            FilterData("本周", "3", "本周"),
            FilterData("本月", "4", "本月"),
            FilterData("上月", "5", "上月"),
            FilterData("本年", "6", "本年"),
            FilterData("自定义时间", "7", "自定义")
        )
    }

    val mUncheckTimeList by lazy {
        listOf(
            FilterData("今日", "1", "今日"),
            FilterData("昨日", "2", "昨日"),
            FilterData("本周", "3", "本周"),
            FilterData("本月", "4", "本月"),
            FilterData("上月", "5", "上月"),
            FilterData("本年", "6", "本年"),
            FilterData("自定义时间", "7", "自定义")
        )
    }

    val mAllTimeList by lazy {
        listOf(
            FilterData("全部", "0", "全部", true),
            FilterData("今日", "1", "今日"),
            FilterData("昨日", "2", "昨日"),
            FilterData("本周", "3", "本周"),
            FilterData("本月", "4", "本月"),
            FilterData("上月", "5", "上月"),
            FilterData("本年", "6", "本年"),
            FilterData("自定义时间", "7", "自定义")
        )
    }

    fun showDropFilter(
        context: Context,
        bottom: View,
        list: List<FilterData>,
        hintTV: TextView? = null,
        anchor: ImageView? = null,
        listener: (FilterData) -> Unit
    ) {
        dropFilter = object : DropPopupWindow(
            context.inflate(R.layout.pop_layout_filter),
            anchor
        ) {

            override fun afterInitView(view: View) {
                val recyclerView = view.find<RecyclerView>(R.id.pop_container)
                recyclerView.apply {
                    loadLinear()
                    addItemDecoration(DividerLRDecoration(bottom = 0.5f))

                    adapter = SlimAdapter.creator()
                        .register<FilterData>(R.layout.item_pop_list) { jector, bean, _ ->

                            // 解构声明
                            val (title, _, hint, check) = bean

                            jector.apply {
                                text(R.id.item_pop_name, title)
                                textColor(
                                    R.id.item_pop_name,
                                    context.getColorEx(
                                        conditionIf(
                                            check,
                                            R.color.colorAccent,
                                            R.color.black
                                        )
                                    )
                                )
                                visibility(
                                    R.id.item_pop_right,
                                    visibleValue { check }
                                )
                                clicked {
                                    hintTV?.text = hint
                                    list.forEach { it.isChecked = false }
                                    bean.isChecked = true
                                    (adapter as SlimAdapter).notifyDataSetChanged()

                                    dropFilter?.dismiss()

                                    delay { listener(bean) }
                                }
                            }
                        }
                        .attachTo(this)

                    (adapter as SlimAdapter).setDataList(list)
                }
            }

        }

        dropFilter?.showAsDropDown(bottom)
    }

    fun showDropTimeFilter(
        context: Context,
        hintTV: TextView? = null,
        anchor: ImageView? = null,
        bottom: View,
        list: List<FilterData>,
        listener: (FilterData) -> Unit
    ) {
        dropFilter = object : DropPopWindow(
            context,
            R.layout.pop_layout_filter,
            anchor
        ) {

            override fun afterInitView(view: View) {
                val recyclerView = view.find<RecyclerView>(R.id.pop_container)
                recyclerView.apply {
                    loadLinear()
                    addItemDecoration(DividerLRDecoration(bottom = 0.5f))

                    adapter = SlimAdapter.creator()
                        .register<FilterData>(R.layout.item_pop_list) { jector, bean, _ ->

                            // 解构声明
                            val (title, _, hint, check) = bean

                            jector.apply {
                                text(R.id.item_pop_name, title)
                                textColor(
                                    R.id.item_pop_name,
                                    context.getColorEx(
                                        conditionIf(
                                            check,
                                            R.color.colorAccent,
                                            R.color.black
                                        )
                                    )
                                )
                                visibility(
                                    R.id.item_pop_right,
                                    visibleValue { check }
                                )
                                clicked {
                                    (bean.id != "7")
                                        .condition {
                                            hintTV?.text = hint
                                            list.forEach { it.isChecked = false }
                                            bean.isChecked = true
                                            (adapter as SlimAdapter).notifyDataSetChanged()
                                        }

                                    dropFilter?.dismiss()

                                    delay { listener(bean) }
                                }
                            }
                        }
                        .attachTo(this)

                    (adapter as SlimAdapter).setDataList(list)
                }
            }

        }

        dropFilter?.showAsDropDown(bottom)
    }

    override fun onDestroy(owner: LifecycleOwner) {
        dropFilter = null
    }

}